﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JieBooLaw.Common.Security;
using JieBooLaw.Common.Web;
using JieBooLaw.Domain;
using JieBooLaw.Service;
using Ext.Net;
using JieBooLaw.Common.Constant;

namespace JieBooLaw.WebAdmin
{
    public partial class Login : BaseWebPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
#if DEBUG
            if (!IsPostBack)
            {
                txtUserName.Text = "administrator";
                txtPassword.Text = "admin1";
            }
#endif
        }

        protected override void ValidatePage()
        {
            base.ValidatePage();

            if (string.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                this.ValidObj.Message = "Please enter your username!";

                return;
            }
            else if (string.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                this.ValidObj.Message = "Please provide your password!";

                return;
            }

            this.ValidObj.IsValid = true;
        }

        private string DoLogin()
        {
            string strMessage = string.Empty;
            string strUserID = txtUserName.Text.Trim();
            string strPWD = Cryptographer.EncryptorAESToString(txtPassword.Text.Trim());

            TSys_UserSvc sysUserSvc = new TSys_UserSvc();

            TSys_User paraUser = new TSys_User() { Account = strUserID, Password = strPWD };
            IList<TSys_User> user = sysUserSvc.GetByEntity(paraUser);
            if (user.Count == 1)
            {   // Login
                if (user.First().Active.HasValue && user.First().Active.Value)
                {
                    user.First().LastLoginTime = DateTime.Now;
                    sysUserSvc.Save(user.First());
                    Session.Add(StringConstant.LoginUserInfo, user.First());
                    // Set Login User
                    this.Redirect("Main.aspx");
                }
                else
                {
                    strMessage = "Your username has been disabled. Please contact your system administrator.";
                }
            }
            else
            {
                strMessage = "Please confirm whether the correct user name password!";
            }

            return strMessage;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.ValidatePage();

            if (this.ValidObj.IsValid == false)
            {
                X.MessageBox.Alert("NONE", this.ValidObj.Message).Show();
                return;
            }

            // Execute the Login Method.
            string loginMessage = DoLogin();
            if (!string.IsNullOrEmpty(loginMessage))
            {
                X.MessageBox.Alert("WARNING", loginMessage).Show();
                return;
            }
        }
    }
}
